<template>
  <el-card>
    <el-button class="addRoles" type="primary" @click="handleDialogValue()"
      >添加角色</el-button
    >

    <el-table :data="rolesList" style="width: 100%" border>
      <el-table-column type="expand">
        <template #default="props">
          <p>State: {{ props.row.state }}</p>
          <p>City: {{ props.row.city }}</p>
          <p>Address: {{ props.row.address }}</p>
          <p>Zip: {{ props.row.zip }}</p>
        </template>
      </el-table-column>
      <el-table-column type="index" />
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="角色描述" prop="roleDesc" />
      <el-table-column label="操作" prop="name" width="300">
        <template #default="{ row }">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="handleDialogValue(row)"
            >编辑</el-button
          >
          <el-button type="warning" size="small" :icon="Setting"
            >分配权限</el-button
          >
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            @click="delRoles(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    v-if="dialogVisible"
    @initUserList="initGetRolesList"
    :dialogTitleValue="dialogTitleValue"
  />
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getRoles, deleteRoles } from '@/api/roles'
import { Edit, Setting, Delete } from '@element-plus/icons-vue'
import Dialog from './components/dialog.vue'
import { isNull } from '@/utils/filters'
import { useI18n } from 'vue-i18n'
const i18n = useI18n()
const rolesList = ref([])
// 初始化角色管理
const initGetRolesList = async () => {
  const res = await getRoles()
  rolesList.value = res
  // console.log(res, '角色权限')
}
initGetRolesList()

// 监听添加用户关闭弹窗
const dialogVisible = ref(false)

// 定一个可修改弹窗的标题
const dialogTitle = ref('')
const dialogTitleValue = ref({})

// 添加角色弹窗
const handleDialogValue = (row) => {
  // 这里还没写完是编辑还是添加用户
  if (isNull(row)) {
    dialogTitle.value = '添加角色'
    dialogTitleValue.value = {}
  } else {
    dialogTitle.value = '编辑角色'
    dialogTitleValue.value = JSON.parse(JSON.stringify(row))
  }
  dialogVisible.value = true
}

// 删除角色事件
const delRoles = (row) => {
  ElMessageBox.confirm(
    i18n.t('dialog.deleteTitle'),
    'Warning',
    {
      confirmButtonText: 'OK',
      cancelButtonText: 'Cancel',
      type: 'warning'
    }
  )
    .then(async () => {
      // 发起删除请求
      await deleteRoles(row)
      ElMessage({
        type: 'success',
        message: '删除角色成功！'
      })
      // 删除成功更新数据
      initGetRolesList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除角色！'
      })
    })
}
</script>

<style lang="scss" scoped>
.addRoles {
  margin-bottom: 20px;
}
</style>
